package com.fg.pro.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fg.pro.sys.dao.SysMenuDao;
import com.fg.pro.model.sys.entity.SysMenu;
import com.fg.pro.sys.service.SysMenuService;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import static com.fg.pro.sys.constant.SysMenuConst.*;

/**
 * 菜单权限(SysMenu)表服务实现类
 *
 * @author makejava
 * @since 2025-07-17 10:10:47
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenu> implements SysMenuService {

    @Override
    public List<SysMenu> getTree() {
        List<SysMenu> firList = query().eq("menu_lev", 1).orderByAsc("menu_sort").list();
        for (SysMenu sysMenu : firList) {
            List<SysMenu> secList = query().eq("menu_lev", 2).eq("menu_parent_id", sysMenu.getId()).orderByAsc("menu_sort").list();
            sysMenu.setChildren(secList);
            if (!secList.isEmpty()) {
                for (SysMenu sysMenus : secList) {
                    List<SysMenu> tirList = query().eq("menu_lev", 3).eq("menu_parent_id", sysMenus.getId()).orderByAsc("menu_sort").list();
                    sysMenus.setChildren(tirList);
                }
            }
        }
        return firList;
    }

    @Override
    public List<Long> getMenuIdById(Long roleId) {
        List<SysMenu> menuList = this.baseMapper.getMenuByRoleId(roleId);
        LinkedList<Long> resultList = new LinkedList<>();
        for (SysMenu sysMenu : menuList) {
            if (sysMenu.getMenuLev() == MENU_LEV_TIR) {
                resultList.add(sysMenu.getId());
            } else if (sysMenu.getMenuLev() == MENU_LEV_SEC) {
                List<SysMenu> tirAllMenuList = query().eq("menu_parent_id", sysMenu.getId()).eq("menu_lev", MENU_LEV_TIR).list();
                List<SysMenu> tirMenuList = menuList.stream().filter(item -> item.getMenuLev() == MENU_LEV_TIR).filter(item -> item.getMenuParentId() == sysMenu.getId()).collect(Collectors.toList());
                if (tirAllMenuList.size() == tirMenuList.size()) {
                    resultList.add(sysMenu.getId());
                }
            } else if (sysMenu.getMenuLev() == MENU_LEV_FIR) {
                LinkedList<Long> allMenuIdListByFir = new LinkedList<>();
                LinkedList<Long> menuIdListByFir = new LinkedList<>();

                List<SysMenu> allListByFir = query().eq("menu_parent_id", sysMenu.getId()).eq("menu_lev", MENU_LEV_SEC).list();
                for (SysMenu secMenu : allListByFir) {
                    allMenuIdListByFir.add(secMenu.getId());
                    List<SysMenu> tirAllMenuList = query().eq("menu_parent_id", secMenu.getId()).eq("menu_lev", MENU_LEV_TIR).list();
                    for (SysMenu tirMenu : tirAllMenuList) {
                        allMenuIdListByFir.add(tirMenu.getId());
                    }
                }

                List<SysMenu> firMenuList = menuList.stream().filter(item -> item.getMenuLev() == MENU_LEV_SEC).filter(item -> item.getMenuParentId() == sysMenu.getId()).collect(Collectors.toList());
                for (SysMenu secMenu : firMenuList) {
                    menuIdListByFir.add(secMenu.getId());
//                    List<SysMenu> tirMenuList = query().eq("menu_parent_id", secMenu.getId()).eq("menu_lev", MENU_LEV_TIR).list();
                    List<SysMenu> tirMenuList = menuList.stream().filter(item -> item.getMenuLev() == MENU_LEV_FIR).filter(item -> item.getMenuParentId() == secMenu.getId()).collect(Collectors.toList());
                    for (SysMenu tirMenu : tirMenuList) {
                        menuIdListByFir.add(tirMenu.getId());
                    }
                }
                if (allMenuIdListByFir.size() == menuIdListByFir.size()) {
                    resultList.add(sysMenu.getId());
                }

            }
        }
        return resultList;
    }
}

